package com.ruoyi.system.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.PAskType;
import com.ruoyi.system.domain.bo.PAskTypeBo;
import com.ruoyi.system.domain.vo.PAskTypeVo;
import com.ruoyi.system.mapper.PAskTypeMapper;
import com.ruoyi.system.mapper.PQuestionMapper;
import com.ruoyi.system.service.IPAskTypeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 咨询类别Service业务层处理
 *
 * @author ruoyi
 * @date 2022-07-13
 */
@RequiredArgsConstructor
@Service
public class PAskTypeServiceImpl implements IPAskTypeService {

    private final PAskTypeMapper baseMapper;
    private final PQuestionMapper pQuestionMapper;

    /**
     * 查询咨询类别
     */
    @Override
    public PAskTypeVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询咨询类别列表
     */
    @Override
    public TableDataInfo<PAskTypeVo> queryPageList(PAskTypeBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<PAskType> lqw = buildQueryWrapper(bo);
        Page<PAskTypeVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询咨询类别列表
     */
    @Override
    public List<PAskTypeVo> queryList(PAskTypeBo bo) {
        LambdaQueryWrapper<PAskType> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<PAskType> buildQueryWrapper(PAskTypeBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<PAskType> lqw = Wrappers.lambdaQuery();
        lqw.eq(StringUtils.isNotBlank(bo.getTitle()), PAskType::getTitle, bo.getTitle());
        lqw.eq(StringUtils.isNotBlank(bo.getRedNum()), PAskType::getRedNum, bo.getRedNum());
        return lqw;
    }

    /**
     * 新增咨询类别
     */
    @Override
    public Boolean insertByBo(PAskTypeBo bo) {
        PAskType add = BeanUtil.toBean(bo, PAskType.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改咨询类别
     */
    @Override
    public Boolean updateByBo(PAskTypeBo bo) {
        PAskType update = BeanUtil.toBean(bo, PAskType.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(PAskType entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除咨询类别
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<PAskType> getSelectList() {
        return baseMapper.selectFontList();
    }

    @Override
    public PAskType getSelectDetail(Long typeId) {
        return baseMapper.selectByTypeId(typeId);
    }

    @Override
    public void updateAskTypeReadNum() {
        List<PAskType> list = this.baseMapper.selectList();
        for (PAskType pAskType: list) {
            this.baseMapper.update(null, new LambdaUpdateWrapper<PAskType>()
                .set(PAskType::getRedNum, this.pQuestionMapper.selectSumReadNum(pAskType.getId())).eq(PAskType::getId, pAskType.getId()));
        }
    }
}
